var noGroup = "(No Groups assigned yet)";

function addGroup(){
    var avail = document.getElementById("availList");
    var grp = avail.options[avail.selectedIndex].innerHTML;
    if (grp=="Select Group...") {
	alert("Please select a Group from the dropdown box to add this User to.");
    } else {
	var ul = document.getElementById("grouplist");
	if (ul.firstChild.innerHTML==noGroup){
		ul.removeChild(ul.firstChild); }
	var rem = document.getElementById("removeList");

	for (i=1;i<avail.length;i++){
	    if(avail[i].innerHTML==grp){
		avail.removeChild(avail[i]);
		var newrem = document.createElement("option");
		  newrem.innerHTML = grp;
		    rem.appendChild(newrem);
	    }
	}
	var newli = document.createElement("li");
	  newli.innerHTML = grp;
	    ul.appendChild(newli); 

    }
}

function remGroup(){
    var rem = document.getElementById("removeList");
    var grp = rem.options[rem.selectedIndex].innerHTML;
    if (grp=="Select Group...") {
	alert("Please select a Group from the dropdown box to remove this User from.");
    } else{
	for (i=1;i<rem.length;i++){
	    if(rem[i].innerHTML==grp){
		rem.removeChild(rem[i]);
	    }
	}

	var ul = document.getElementById("grouplist");
	for (i=0;i<ul.childNodes.length;i++){
	    if(ul.childNodes[i].innerHTML==grp){
		ul.removeChild(ul.childNodes[i]);
	    }
	}

	if (ul.childNodes.length==0){
	    var newli = document.createElement("li");
	      newli.innerHTML = noGroup;
	        ul.appendChild(newli);
	}
	
	ul = document.getElementById("availList");
	var newava = document.createElement("option");
	  newava.innerHTML = grp;
	    ul.appendChild(newava);
    }
}

function submitUser(){
    var title = document.getElementById("pageTitle");
//NEW----------------------------------------------------------------------------
    if (!checkFieldsForContent()) return;
//----------------------------------------------------------------------------
    var name = document.getElementById("username");
    var password = document.getElementById("password1");
    var email = document.getElementById("email");
    var allGroups = "";
    var groups = document.getElementById("grouplist");
    if (groups.childNodes[0].innerHTML==noGroup) { allGroups = ""; }
    else{
	for (i=0;i<groups.childNodes.length;i++){
	    allGroups = allGroups + groups.childNodes[i].innerHTML + ";";
	} allGroups = allGroups.substring(0,allGroups.length-1); //remove trailing ;
    }
    if (title.innerHTML=="Add New User"){
	if (!checkPasswords()) return;
	var queryString = "username=" + name.value + "&password=" + password.value + "&email=" + email.value + "&groups=" + allGroups;	
	document.location.href = "UserAdded?" + queryString;
    } else { // edit existing User
	if (document.getElementById("passFlag").value=="true" && !checkPasswords()) return;
	var passwordValue = password.value;
        var queryString;
        if (document.getElementById("passFlag").value=="true") {
            queryString = "username=" + name.value + "&password=" + passwordValue + "&email=" + email.value + "&groups=" + allGroups + "&oldUsername=" + document.getElementById("oldUsername").value + "&passFlag=true";	
        } else {
            queryString = "username=" + name.value + "&password=" + passwordValue + "&email=" + email.value + "&groups=" + allGroups + "&oldUsername=" + document.getElementById("oldUsername").value + "&passFlag=false";
        }
	document.location.href = "UserEdited?" + queryString;
    }
}

function checkPasswords(){
    var pwd1 = document.getElementById("password1");
    var pwd2 = document.getElementById("password2");
    if (pwd1.value != pwd2.value) {
	resetPasswords();
	return false; 
    } return true;
}

function resetPasswords(){
    alert("Passwords do not match! Please re-enter your password.");
    document.getElementById("password1").value="";
    document.getElementById("password2").value="";
    document.getElementById("password1").focus();
}

//NEW----------------------------------------------------------------------------
function checkFieldsForContent(){
    if (checkLength("username", 32, "username", true) == false) return false;
    if (checkLength("email", 32, "E-mail", false) == false) return false;
    if (checkSyntax("username","username") == false) return false;
    if (checkSyntax("email","E-mail") == false) return false;

    var check = document.getElementById("password1").value;
    if (check == "") {
	var ans = confirm("Are you sure you don't want to use a password for this User?");
	if (ans!=true){
	    return false;
	}	
    }
    return true;
}//----------------------------------------------------------------------------

function changePassword(){
    var passFlag = document.getElementById("passFlag");
    var userPass = document.getElementById("userPass");
    var pass1 = document.getElementById("password1");
    if (passFlag.value == "false") {
	var newRow = document.createElement("tr");
	var newCol1 = document.createElement("td");
	var newCol0 = document.createElement("td");
	var pass2 = document.createElement("input");
	var oldPass = document.createElement("input");
	    newRow.id = "newRow";
	    newCol0.innerHTML = "Re-type Password:";
	    oldPass.value = pass1.value;
	    oldPass.setAttribute('type', "hidden");
	    oldPass.setAttribute('id',"oldPass");
	    pass2.type = "password";
	    pass2.setAttribute('id',"password2");
	    pass2.setAttribute('style',"width:250px");
	    pass1.type = "password";
	    pass1.readOnly = false;
	    pass1.value="";
		newCol1.appendChild(pass2);
		newCol1.appendChild(oldPass);
		newRow.appendChild(newCol0);
		newRow.appendChild(newCol1);
		userPass.insertBefore(newRow,document.getElementById("emailRow"));
	document.getElementById("passButton").value = "Use Old Password";
	passFlag.value = "true";
    }
    else {
	var newRow = document.getElementById("newRow");
	pass1.value = document.getElementById("oldPass").value;
	pass1.type = "text";
	pass1.readOnly = true;
	document.getElementById("passButton").value = "Change...";
	userPass.removeChild(newRow);
	passFlag.value = "false";
    }
}

function directToEdit(queryString){
    document.location.href = "EditUser" + queryString;
}

function directToAdd(queryString){
    document.location.href = "AddUser" + queryString;
}


//NEW----------------------------------------------------------------------------
function checkSyntax(field, str){
    var check = document.getElementById(field).value;
    if (check.match(/^[\.\_\@\-A-Za-z]+$/)) {
	return true;
    } else{
	var bad = "";
	for (var i=0;i<check.length;i++){
	    if (!check.charAt(i).match(/^[\.\_\@\-A-Za-z]+$/)) { 
		if (check.charAt(i) == " ")  {
		    bad += "\t(space) (character " + i + ")\n";
		} else {
		    bad += "\t" + check.charAt(i) + " (character " + i + ")\n";
		}
	    }
	}
	alert("The " + str + " field has an illegal character. Please revise the following:\n" + bad);
	document.getElementById(field).focus();
	return false;
    }
}

function checkLength(field, len, str, mandatory){
    var check = document.getElementById(field);
    if (mandatory == true && check.value == "") {
	alert("Please type a " + str + " for this User.");
	check.focus();
	return false;
    }
    if (check.value.length > len){
	alert("The " + str + " you typed is too long. Please create a " + str + " that is " + len + " characters or less.");
	check.focus();
	return false;
    }
    return true;
}
//----------------------------------------------------------------------------

